<?php

namespace App\Containers\AppSection\Pledge\UI\API\Transformers;

use App\Containers\AppSection\Pledge\Models\Pledge;
use App\Containers\AppSection\Project\UI\API\Transformers\ProjectTransformer;
use App\Containers\AppSection\Type\UI\API\Transformers\TypeTransformer;
use App\Ship\Parents\Transformers\Transformer;
use League\Fractal\Resource\Item;

class PledgeTransformer extends Transformer
{
    protected array $defaultIncludes = [
        'property'
    ];

    /**
     * @var  array
     */
    protected array $availableIncludes = [
        'project', 'rating'
    ];

    public function transform(Pledge $t): array
    {
        return [
            'object' => $t->getResourceKey(),
            'include' => $this->getAvailableIncludes(),
            'id' => $t->getHashedKey(),
            'property_type' => $t->property_type,
            'collateral_value' => $t->collateral_value,
            'description' => $t->description,
            'created_at' => $this->dateFormatter($t->created_at),
            'updated_at' => $this->dateFormatter($t->updated_at),
            'readable_created_at' => $t->created_at->diffForHumans(),
            'readable_updated_at' => $t->updated_at->diffForHumans()
        ];
    }

    public function includeProperty(Pledge $t): Item
    {
        switch ($t->property_type) {
            case 'property' :
                return $this->item($t->property, new PropertyTransformer);
            case 'ddu' :
                return $this->item($t->property, new DDUTransformer);
            case 'other' :
                return $this->item($t->property, new OtherTransformer);
            case 'movable' :
            default :
                return $this->item($t->property, new MovableTransformer);
        }
    }

    public function includeProject(Pledge $t): Item
    {
        return $this->item($t->project, new ProjectTransformer);
    }

    public function includeRating(Pledge $t): Item
    {
        return $this->item($t->rating, new TypeTransformer);
    }
}
